<?php
/**
 * Created by PhpStorm.
 * User: zhangrxiang
 * Date: 2018/5/2
 * Time: 18:02
 */

/**
 * @param array $rows
 */
function p($rows = []){
    $str = "";
    foreach ($rows as $index => $value){
        $str .= sprintf("%-12s| ",$value);
    }
    $str.="\n";
    echo $str;
}
//$mysqli = mysqli_connect("localhost", "root", "root", "test_db", 3306);
$mysqli = mysqli_connect("127.0.0.1", "root", "root", "test_db", 3306) or exit();
$sql = <<<EOL
SELECT 
    lastname, firstname, jobtitle
FROM
    employees
EOL;

$mysqli->real_query($sql);
$mysqli_result = $mysqli->use_result();

printf("%-12s| %-12s| %-12s\n", "lastname", "firstname", "jobtitle");
while ($row = $mysqli_result->fetch_row()) {
    printf("%s\n", str_repeat("-", 40));
//    printf("%-12s| %-12s| %-12s\n", $row[0], $row[1], $row[2]);
    p($row);
}
printf("%s\n", str_repeat("-", 45 * 2));

$sql = <<<SQL
SELECT 
    lastname, firstname, jobtitle
FROM
    employees
WHERE
    jobtitle = 'Sales Rep'; 
SQL;
if($mysqli->real_query($sql)){
    $result = $mysqli->use_result();
    while ($row = $result->fetch_row()) {
        printf("%s\n", str_repeat("-", 40));
        p($row);
    }
}else{
    echo "error\n";
}